package com.moxiao.yfjh.module.maker.controller.admin.makerProductInfo;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.maker.common.GenMakerNoCommonUtil;
import com.moxiao.yfjh.module.maker.controller.admin.makerProductInfo.vo.*;
import com.moxiao.yfjh.module.maker.convert.makerProductInfo.MatchmakerProductInfoConvert;
import com.moxiao.yfjh.module.maker.dal.dataobject.makerProductInfo.MatchmakerProductInfoDO;
import com.moxiao.yfjh.module.maker.service.makerProductInfo.MatchmakerProductInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 相亲红娘商品信息")
@RestController
@RequestMapping("/maker/matchmaker-product-info")
@Validated
public class MatchmakerProductInfoController {

    @Resource
    private MatchmakerProductInfoService matchmakerProductInfoService;
    @Resource
    private GenMakerNoCommonUtil genMakerNoCommonUtil;


    @PostMapping("/create")
    @Operation(summary = "创建相亲红娘商品信息")
    @PreAuthorize("@ss.hasPermission('blind:matchmakerProduct:create')")
    public CommonResult<Long> createMatchmakerProductInfo(@Valid @RequestBody MatchmakerProductInfoCreateReqVO createReqVO) {
        genMakerNoCommonUtil.addProductInfoNo(createReqVO);
        return success(matchmakerProductInfoService.createMatchmakerProductInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲红娘商品信息")
    @PreAuthorize("@ss.hasPermission('blind:matchmakerProduct:update')")
    public CommonResult<Boolean> updateMatchmakerProductInfo(@Valid @RequestBody MatchmakerProductInfoUpdateReqVO updateReqVO) {
        matchmakerProductInfoService.updateMatchmakerProductInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲红娘商品信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:matchmakerProduct:delete')")
    public CommonResult<Boolean> deleteMatchmakerProductInfo(@RequestParam("id") Long id) {
        matchmakerProductInfoService.deleteMatchmakerProductInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲红娘商品信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-product-info:query')")
    public CommonResult<MatchmakerProductInfoRespVO> getMatchmakerProductInfo(@RequestParam("id") Long id) {
        MatchmakerProductInfoDO matchmakerProductInfo = matchmakerProductInfoService.getMatchmakerProductInfo(id);
        return success(MatchmakerProductInfoConvert.INSTANCE.convert(matchmakerProductInfo));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲红娘商品信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-product-info:query')")
    public CommonResult<List<MatchmakerProductInfoRespVO>> getMatchmakerProductInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<MatchmakerProductInfoDO> list = matchmakerProductInfoService.getMatchmakerProductInfoList(ids);
        return success(MatchmakerProductInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲红娘商品信息分页")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-product-info:query')")
    public CommonResult<PageResult<MatchmakerProductInfoRespVO>> getMatchmakerProductInfoPage(@Valid MatchmakerProductInfoPageReqVO pageVO) {
        PageResult<MatchmakerProductInfoDO> pageResult = matchmakerProductInfoService.getMatchmakerProductInfoPage(pageVO);
        return success(MatchmakerProductInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲红娘商品信息 Excel")
    @PreAuthorize("@ss.hasPermission('blind:matchmakerProduct:export')")
    @OperateLog(type = EXPORT)
    public void exportMatchmakerProductInfoExcel(@Valid MatchmakerProductInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MatchmakerProductInfoDO> list = matchmakerProductInfoService.getMatchmakerProductInfoList(exportReqVO);
        // 导出 Excel
        List<MatchmakerProductInfoExcelVO> datas = MatchmakerProductInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲红娘商品信息.xls", "数据", MatchmakerProductInfoExcelVO.class, datas);
    }

}
